package org.jeecg.modules.InveotoryCenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.InveotoryCenter.entity.chukudanEntity;
import org.jeecg.modules.InveotoryCenter.entity.chukugoodsEntity;
import org.jeecg.modules.InveotoryCenter.entity.piciEntity;
import org.jeecg.modules.InveotoryCenter.mapper.ChukuManagerMapper;
import org.jeecg.modules.InveotoryCenter.service.ChukuManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/ChukuManager")
public class ChukuManagerController {

    @Autowired
    ChukuManagerService service;

    @Autowired
    ChukuManagerMapper mapper;

    /**
     * 出库申请列表查询
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/chukushenqinglist")
    public Result<?> chukushenqinglist(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam( defaultValue = "10") int  status,
            String orderSn
    ){
        Page<chukudanEntity> page =new  Page<chukudanEntity>(pageNo, pageSize);
        IPage<chukudanEntity> pageList=service.getchukushenqinglist(page,status,orderSn);
        return Result.ok(pageList);

    }


    /**
     * 出库申请表下的商品表查询
     */
    @GetMapping("/getchukushenqingshangping")
    public Result<?> getchukushenqingshangping(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "id") String id
    ){
        Page<chukugoodsEntity> page =new  Page<chukugoodsEntity>(pageNo, pageSize);
        IPage<chukugoodsEntity> pageList=service.getchukushenqingshangping(page,id);
        return Result.ok(pageList);
    }

    /**
     * 出库商品下的批次
     */
    @GetMapping("/getpicibychukugoods")
    public Result<?> getpicibychukugoods(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "id") String id
    ){
        Page<piciEntity> page =new  Page<piciEntity>(pageNo, pageSize);
        IPage<piciEntity> pageList=service.getpicibychukugoods(page,id);
        return Result.ok(pageList);
    }


    /**
     * 出库商品下的出库明细
     */
    @GetMapping("/getchukumingxibychukugoods")
    public Result<?> getchukumingxibychukugoods(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "id") String id
    ){
        Page<piciEntity> page =new  Page<piciEntity>(pageNo, pageSize);
        IPage<piciEntity> pageList=service.getchukumingxibychukugoods(page,id);
        return Result.ok(pageList);
    }


    /**
     * 出库明细里的确认出库
     */

    @PostMapping("/MinxiEnterchuku")
    public Result<?> MinxiEnterchuku(
            @RequestBody piciEntity entity
    ){
        try{
            int conut=service.MinxiEnterchuku(entity.getGoodsId(),entity.getId(),entity.getOutstorageNumber(),entity.getBatchCode());
            return Result.ok("出库成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.ok("操作失败");
        }
    }

    /**
     * 出库明细里的撤回
     */
    @PostMapping("/Minxichehui")
    public Result<?> Minxichehui(
            @RequestBody piciEntity entity
    ){
        try{
            int conut=service.Minxichehui(entity.getGoodsId(),entity.getId(),entity.getOutstorageNumber(),entity.getBatchCode());
            return Result.ok("撤回成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.ok("操作失败");
        }
    }

    /**
     *  新增出库明细
     */
    @PostMapping("/addchukumingxi")
    public Result<?> addchukumingxi(
            @RequestBody piciEntity entity
    ){
        try{
            //先查询老的批次库存
            String oldpicikucun=mapper.getoldpicikucun(entity.getBatchCode());
            if(Double.parseDouble(entity.getOutstorageNumber())>Double.parseDouble(oldpicikucun)){
                return Result.ok("出库数禁止大于库存数");
            }
            int conut=service.addchukumingxi(entity);
            return Result.ok("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.ok("操作失败");
        }
    }

    /**
     * 查询商品详情
     */
    @GetMapping("/getgoodsdetail")
    public Result<?> getgoodsdetail(
            @RequestParam(name = "goodsId") String goodsId,
            @RequestParam(name = "goodsType") int goodsType
    ){
        HashMap<String,String> map=service.getgoodsdetail(goodsId,goodsType);
        return Result.ok(map);
    }
}
